home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Light Sleeper / Code / Light Sleeper.c < prev   
Encoding:
C/C++ Source or Header  |  2001-06-23  |  5.7 KB  |  320 lines

  1. #include <Power.h>
  2. #include <QDOffscreen.h>
  3. #include <ControlDefinitions.h>
  4. #include <ControlStrip.h>
  5. #include "Light Sleeper.h"
  6. #include "Backlight.h"
  7.  
  8. #define    kPauseLength    30
  9. #define    kMovementWindow    2
  10.  
  11. long MyDrinkJolt( long message, SleepQRecPtr qRecPtr );
  12.  
  13. UniversalProcPtr    gOldGetNextEvent = nil, gNewGetNextEvent;
  14. UniversalProcPtr    gOldFrontWindow = nil, gNewFrontWindow;
  15. UniversalProcPtr    gOldButton = nil, gNewButton;
  16. WindowPtr            myFrontWindow = nil;
  17. Boolean                selectWindow = true, lockButton = false, controlStripAvail = false;
  18. Point                mouseLoc;
  19. short                theRatio = 1;
  20. long                ticks = 0, trash;
  21.  
  22. ControlHandle        theScrollBar;
  23.  
  24. void main( void )
  25. {
  26.     SleepQRec    mySleepRec;
  27.     
  28.     if ( Gestalt( gestaltControlStripVersion, &trash ) == noErr )
  29.         controlStripAvail = true;
  30.     
  31.     InitializeToolbox();
  32.     
  33.     mySleepRec.sleepQType = slpQType;
  34.     mySleepRec.sleepQProc = NewSleepQUPP( MyDrinkJolt );
  35.     SleepQInstall( &mySleepRec );
  36.     
  37.     DrawMenubar();
  38.     
  39.     do
  40.     {
  41.         EventOnce();
  42.     }
  43.     while ( done == false );
  44.     
  45.     SleepQRemove( &mySleepRec );
  46. }
  47.  
  48. void InitializeToolbox( void )
  49. {
  50.     InitGraf( &qd.thePort );
  51.     InitFonts();
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs( 0L );
  56.     FlushEvents( everyEvent, 0 );
  57.     InitCursor();
  58. }
  59.  
  60. void DrawMenubar( void )
  61. {
  62.     Handle        menuBar;
  63.     MenuHandle    menu;
  64.     
  65.     menuBar= GetNewMBar( rMenuBar );
  66.     SetMenuBar( menuBar );
  67.     DrawMenuBar();
  68.     
  69.     menu = GetMenuHandle( mApple );
  70.     AppendResMenu( menu, 'DRVR' );
  71. }
  72.  
  73. void EventOnce( void )
  74. {
  75.     EventRecord    event;
  76.     
  77.     WaitNextEvent( everyEvent, &event, 0x7FFFFFFF, nil );
  78.     
  79.     switch ( event.what )
  80.     {
  81.         case mouseDown:
  82.             HandleMouseDown( &event );
  83.             break;
  84.         case keyDown:
  85.             HandleKeyDown( event );
  86.             break;
  87.         case autoKey:
  88.             HandleKeyDown( event );
  89.             break;
  90.         case updateEvt:
  91.             HandleUpdate( event );
  92.             break;
  93.         case activateEvt:
  94.             break;
  95.     }
  96. }
  97.  
  98. void HandleUpdate( EventRecord theEvent )
  99. {
  100.     WindowPtr    window;
  101.     
  102.     window = (WindowPtr)theEvent.message;
  103.     
  104.     BeginUpdate( window );
  105.         SetPort( window );
  106.         DrawControls( window );
  107.     EndUpdate( window );
  108. }
  109.  
  110. void HandleMouseDown( EventRecord *eventPtr )
  111. {
  112.     WindowPtr        whichWindow, savePort;
  113.     Rect            dragRect = {0,0,3000, 3000};
  114.     Point            thePoint;
  115.     ControlHandle    theControl;
  116.     short            thePart;
  117.     long            menuChoice;
  118.     
  119.     thePart = FindWindow( eventPtr->where, &whichWindow );
  120.     thePoint = (*eventPtr).where;
  121.     switch (thePart)
  122.     {
  123.         case inSysWindow:
  124.             SystemClick( eventPtr, whichWindow );
  125.             break;
  126.         case inMenuBar:
  127.             menuChoice = MenuSelect( eventPtr->where );
  128.             if ( menuChoice != 0 )
  129.                 HandleMenuChoice( menuChoice );
  130.         case inDrag:
  131.             DragWindow( whichWindow, eventPtr->where, &dragRect);
  132.             break;
  133.         case inGoAway:
  134.             if ( TrackGoAway(whichWindow, eventPtr->where) == true)
  135.                 DisposeWindow(whichWindow);
  136.             break;
  137.         case inContent:
  138.             if ( whichWindow != FrontWindow() )
  139.             {
  140.                 SelectWindow( whichWindow );
  141.                 return;
  142.             }
  143.             
  144.             GetPort( &savePort );
  145.             SetPort( whichWindow );
  146.             GlobalToLocal( &thePoint );
  147.             SetPort( savePort );
  148.             theControl = FindControlUnderMouse( thePoint, whichWindow, &thePart );
  149.             thePart = HandleControlClick( theControl, thePoint, (*eventPtr).modifiers, (ControlActionUPP)-1L );
  150.             
  151.             break;
  152.     }
  153.     HiliteMenu( 0 );
  154. }
  155.  
  156. void HandleAppleChoice( short item )
  157. {
  158.     MenuHandle        appleMenu;
  159.     Str255            accName;
  160.     short            accNumber;
  161.     
  162.     switch ( item )
  163.     {
  164.         case iAbout:
  165.             AboutDialog();
  166.             break;
  167.         default:
  168.             appleMenu = GetMenuHandle( mApple );
  169.             GetMenuItemText( appleMenu, item, accName );
  170.             accNumber = OpenDeskAcc( accName );
  171.             break;
  172.     }
  173. }
  174.  
  175. void HandleFileChoice( short item )
  176. {
  177.     switch ( item )
  178.     {
  179.         case iQuit:
  180.             done = true;
  181.             break;
  182.     }
  183. }
  184.  
  185. void HandleKeyDown( EventRecord theEvent )
  186. {
  187.     short    theChar;
  188.     long    theChoice;
  189.     
  190.     theChar = theEvent.message & charCodeMask;
  191.     
  192.     if ( ( theEvent.modifiers & cmdKey ) != 0 )
  193.     {
  194.         if ( theEvent.what != autoKey )
  195.         {
  196.             theChoice = MenuKey( theChar );
  197.             HandleMenuChoice( theChoice );
  198.         }
  199.     }
  200. }
  201.  
  202. void HandleMenuChoice( long theChoice )
  203. {
  204.     short theMenu;
  205.     short theMenuItem;
  206.     
  207.     theMenu = HiWord( theChoice );
  208.     theMenuItem = LoWord( theChoice );
  209.     switch ( theMenu )
  210.     {
  211.         case mApple:
  212.             HandleAppleChoice( theMenuItem );
  213.             break;
  214.         case mFile:
  215.             HandleFileChoice( theMenuItem );
  216.             break;
  217.     }
  218.     HiliteMenu( 0 );
  219. }
  220.  
  221. void AboutDialog( void )
  222. {
  223.     GrafPtr            savePort;
  224.     DialogPtr        dlog;
  225.     short            item;
  226.     
  227.     GetPort( &savePort );
  228.     
  229.     dlog = GetNewDialog( 128, 0L, (WindowPtr) -1L );
  230.     
  231.     SetDialogDefaultItem( dlog, 1 );
  232.     SetPort( dlog );
  233.     
  234.     if ( dlog )
  235.     {
  236.         do
  237.         {
  238.             ModalDialog( nil, &item );
  239.         }
  240.         while ( item != 1 );
  241.         
  242.         DisposeDialog(dlog);
  243.     }
  244.     
  245.     SetPort( savePort );
  246. }
  247.  
  248. long MyDrinkJolt( long message, SleepQRecPtr qRecPtr )
  249. {
  250.     #pragma unused ( qRecPtr )
  251.     
  252.     WindowPtr        theWindow;
  253.     Boolean            controlStripVis;
  254.     short            speed = 1;
  255.     int                ii;
  256.     long            tickCount;
  257.     unsigned long    finalTicks;
  258.     
  259.     if ( message != sleepRequest && message != sleepDemand )
  260.         return 0;
  261.     
  262.     InitializeBacklightControl();
  263.     
  264.     if ( controlStripAvail )
  265.     {
  266.         controlStripVis = SBIsControlStripVisible();
  267.         SBShowHideControlStrip( false );
  268.     }
  269.     HideMenuBar();
  270.     HideCursor();
  271.     
  272.     theWindow = GetNewCWindow( 128, nil, (WindowPtr)-1 );
  273.     SizeWindow( theWindow, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom, true );
  274.     
  275.     do
  276.     {
  277.         if(speed < 1)
  278.             speed = 1;
  279.             
  280.         for(ii = 0; ii <= 60; ii += speed)
  281.         {
  282.             tickCount = LMGetTicks();
  283.             
  284.             Delay( 2, &finalTicks );
  285.             
  286.             if(ii > 60)
  287.                 ii = 60;
  288.             SetBacklightBrightness(31 - ii/2);
  289.             while(LMGetTicks() == tickCount) {};
  290.         }
  291.         
  292.         if(speed < 1)
  293.         speed = 1;
  294.  
  295.         for(ii = 0; ii < 60 + speed; ii += speed)
  296.         {
  297.             tickCount = LMGetTicks();
  298.             
  299.             Delay( 2, &finalTicks );
  300.             
  301.             if(ii > 60)
  302.                 ii = 60;
  303.             SetBacklightBrightness(2 + ii/2);
  304.             while(LMGetTicks() == tickCount) {};
  305.         }
  306.     }
  307.     while( !Button() );
  308.     
  309.     DisposeWindow( theWindow );
  310.     
  311.     if ( controlStripAvail )
  312.     {
  313.         SBShowHideControlStrip( controlStripVis );
  314.     }
  315.     
  316.     ShowMenuBar();
  317.     InitCursor();
  318.     
  319.     return 1;
  320. }